// m

// 给定一个已排序的链表的头head
// 要求删除原始链表中所有重复数字的节点，只留下不同的数字，返回已排序的链表

const { ListNode, LinkedList } = require('../../1. 链表基础/1. 建立线性链表')

function deleteDuplicates(head) {
    // 构造一个哑结点，指向head，防止head本身就是重复元素
    let dummy_head = new ListNode(-1)
    dummy_head.next = head

    let cur = dummy_head
    while (cur.next && cur.next.next) {
        if (cur.next.val === cur.next.next.val) {
            let temp = cur.next
            while (temp && temp.next && temp.val === temp.next.val) {
                temp = temp.next
            }
            cur.next = temp.next
        } else {
            cur = cur.next
        }
    }
    return dummy_head.next
}

let head = [1, 1, 1, 2, 3]
let node = new LinkedList(head)
console.log(deleteDuplicates(node.head));